package cn.iocoder.an.module.business.controller.admin.producttablemapping;

import cn.iocoder.an.framework.tenant.core.aop.TenantIgnore;
import com.baomidou.dynamic.datasource.annotation.Slave;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.producttablemapping.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.producttablemapping.ProductTableMappingDO;
import cn.iocoder.an.module.business.service.producttablemapping.ProductTableMappingService;

@Tag(name = "管理后台 - 产品表名映射")
@RestController
@RequestMapping("/business/product-table-mapping")
@Validated
public class ProductTableMappingController {

    @Resource
    private ProductTableMappingService productTableMappingService;

    @TenantIgnore
    @Slave
    @PostMapping("/create")
    @Operation(summary = "创建产品表名映射")
    public CommonResult<String> createProductTableMapping(@Valid @RequestBody ProductTableMappingSaveReqVO createReqVO) {
        return success(productTableMappingService.createProductTableMapping(createReqVO));
    }

    @TenantIgnore
    @Slave
    @PutMapping("/update")
    @Operation(summary = "更新产品表名映射")
    public CommonResult<Boolean> updateProductTableMapping(@Valid @RequestBody ProductTableMappingSaveReqVO updateReqVO) {
        productTableMappingService.updateProductTableMapping(updateReqVO);
        return success(true);
    }

    @TenantIgnore
    @Slave
    @DeleteMapping("/delete")
    @Operation(summary = "删除产品表名映射")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteProductTableMapping(@RequestParam("id") String id) {
        productTableMappingService.deleteProductTableMapping(id);
        return success(true);
    }

    @TenantIgnore
    @Slave
    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除产品表名映射")
    public CommonResult<Boolean> deleteProductTableMappingList(@RequestParam("ids") List<String> ids) {
        productTableMappingService.deleteProductTableMappingListByIds(ids);
        return success(true);
    }

    @TenantIgnore
    @Slave
    @GetMapping("/get")
    @Operation(summary = "获得产品表名映射")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ProductTableMappingRespVO> getProductTableMapping(@RequestParam("id") String id) {
        ProductTableMappingDO productTableMapping = productTableMappingService.getProductTableMapping(id);
        return success(BeanUtils.toBean(productTableMapping, ProductTableMappingRespVO.class));
    }

    @TenantIgnore
    @Slave
    @GetMapping("/page")
    @Operation(summary = "获得产品表名映射分页")
    public CommonResult<PageResult<ProductTableMappingRespVO>> getProductTableMappingPage(@Valid ProductTableMappingPageReqVO pageReqVO) {
        PageResult<ProductTableMappingDO> pageResult = productTableMappingService.getProductTableMappingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductTableMappingRespVO.class));
    }

    @TenantIgnore
    @Slave
    @GetMapping("/export-excel")
    @Operation(summary = "导出产品表名映射 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductTableMappingExcel(@Valid ProductTableMappingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductTableMappingDO> list = productTableMappingService.getProductTableMappingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品表名映射.xls", "数据", ProductTableMappingRespVO.class,
                        BeanUtils.toBean(list, ProductTableMappingRespVO.class));
    }

}